package com.mandao.oneplat.boot.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by sunson on 2017/3/24.
 */
public final class SecurityUtil {

    static String AES = "AES";
    static byte[] AES_KEY = "8ffca057abae1fcb23506f288764250d".getBytes();

    public static String AESEncode(String data) {
        try {
            SecretKey secretKey = new SecretKeySpec(AES_KEY, AES);//恢复密钥
            Cipher cipher = Cipher.getInstance(AES);//Cipher完成加密或解密工作类
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);//对Cipher初始化，解密模式
            byte[] cipherByte = cipher.doFinal(data.getBytes());//加密data
            return ByteUtil.bytes2hex(cipherByte);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String AESDecode(String data) {
        try {
            SecretKey secretKey = new SecretKeySpec(AES_KEY, AES);//恢复密钥
            Cipher cipher = Cipher.getInstance(AES);//Cipher完成加密或解密工作类
            cipher.init(Cipher.DECRYPT_MODE, secretKey);//对Cipher初始化，解密模式
            byte[] cipherByte = cipher.doFinal(ByteUtil.hex2bytes(data));//解密data
            return new String(cipherByte);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
